﻿/// <reference path="UI.js" />
/// <reference path="Settings.js" />
/// <reference path="data.js" />
/// <reference path="../pages/rssSettings/rssSettingsPage.js" />
// For an introduction to the Grid template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=232446
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var nav = WinJS.Navigation;
    var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();

    function refreshRSS() {
        nav.navigate("../pages/groupedItems/groupedItems.html", {});
        Data.Manager.refreshRSS();
    };

    function shareFileHandler(event) {
        var dataRequest = event.request;

        dataRequest.data.properties.title = "RSS Settings JSON";
        dataRequest.data.properties.description = "RSS Settings as a JSON text file.";

        dataRequest.data.properties.fileTypes.replaceAll([".json"]);

        var deferral = dataRequest.getDeferral();

        var text = JSON.stringify(Settings.Manager.getRoamingSettings());

        var localFolder = Windows.Storage.ApplicationData.current.localFolder;

        localFolder.createFileAsync("RssSettings.json", Windows.Storage.CreationCollisionOption.replaceExisting)
            .done(
        function (file) {
            Windows.Storage.FileIO.writeTextAsync(file, text)
                .done(
            function () {
                dataRequest.data.setStorageItems([file]);
                deferral.complete();
            },
            function (error) {
                UI.alert("Couldn't share file!");
            });
        },
        function (error) {
            UI.alert("Couldn't share file!");
        });
    }

    app.addEventListener("activated", function (args) {
        
        dataTransferManager.addEventListener("datarequested", shareFileHandler);

        document.getElementById("rssSettingsCommand").addEventListener("click", function () {
            Settings.Manager.navigateToRssSettings();
        }, false);

        document.getElementById("privacyPolicyCommand").addEventListener("click", function () {
            nav.navigate("../pages/privacyPolicy/privacyPolicyPage.html", {});
        }, false);

        document.getElementById("refreshCommand").addEventListener("click", function () {
            refreshRSS();
        }, false);

        if (args.detail.kind === activation.ActivationKind.launch) {

            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. If you need to 
        // complete an asynchronous operation before your application is 
        // suspended, call args.setPromise().
        app.sessionState.history = nav.history;
    };

    WinJS.Application.onsettings = function (e) {
        e.detail.applicationcommands = { "gameSettings": { title: "Privacy policy", href: "/pages/privacySettings/privacySettingsPage.html" } };
        WinJS.UI.SettingsFlyout.populateSettings(e);
    };

    app.start();
})();
